﻿namespace EyeOpen.TimeAssistant.Client.Wcf
{
    using System;
    using AuthenticationServiceReference;
    using EyeOpen.TimeAssitant.Client.Service;

    public class AsynchronousAuthenticationService : IAsynchronousAuthenticationService
    {
        private readonly AuthenticationServiceClient authenticationServiceClient;

        public AsynchronousAuthenticationService()
        {
            authenticationServiceClient = new AuthenticationServiceClient();
            authenticationServiceClient.AuthenticateUserCompleted += AuthenticateUserCompleted;
        }

        public event EventHandler<AuthenticateEventArgs> AuthenticationLoaded;

        private void AuthenticateUserCompleted(object sender, AuthenticationServiceReference.AuthenticateUserCompletedEventArgs e)
        {
            var handler = AuthenticationLoaded;

            if (handler != null)
            {
                handler(this, new AuthenticateEventArgs(e.Result));
            }
        }

        public void AuthenticateUser(string username, string password)
        {
            authenticationServiceClient.AuthenticateUserAsync(username, password);
        }
    }
}
